package com.hgw.Demo04_CreateInterator.examples;

import java.util.List;

/**
 * Data time:2021/12/18 09:53
 * StudentID:2019112118
 * Author:hgw
 * Description: 商品迭代器，充当具体迭代器
 */
public class ProductIterator implements AbstractIterator{
    private List<Object> products;
    private int cursor1;            // 定义一个游标，用于记录正向遍历的位置
    private int cursor2;            // 定义一个游标，用于记录逆向遍历的位置

    public ProductIterator(ProductList list) {
        this.products = list.getObjects();      // 获取集合对象
        cursor1 = 0;                            // 设置正向遍历游标的初始值
        cursor2 = products.size() - 1;          // 设置逆向遍历游标的初始值
    }

    @Override
    public void next() {
        if (cursor1 < products.size()) {
            cursor1++;
        }
    }

    @Override
    public boolean isLast() {
        return (cursor1==products.size());
    }

    @Override
    public void previous() {
        if (cursor2 > -1){
            cursor2--;
        }
    }

    @Override
    public boolean isFirst() {
        return (cursor2==-1);
    }

    @Override
    public Object getNextItem() {
        return products.get(cursor1);
    }

    @Override
    public Object getPreviousItem() {
        return products.get(cursor2);
    }
}
